<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>常见事件案例</title>
</head>

<body>
    <img id="light" src="img/off.gif"> <br>
    <input type="button" value="点亮" onclick="on()"> <input type="button" value="熄灭" onclick="off()">

    <br> <br>

    <input type="text" id="name" value="ITCAST" onfocus="lower()" onblur="upper()">
    <br> <br>

    <input type="checkbox" name="hobby"> 电影
    <input type="checkbox" name="hobby"> 旅游
    <input type="checkbox" name="hobby"> 游戏
    <br>

    <input type="button" value="全选" onclick="checkAll()"> <input type="button" value="反选" onclick="reverse()">

    <script>
        //1.点击点亮，把灯点亮；点击熄灭，把灯熄灭
        function on() {
            var img = document.getElementById('light');
            img.src = "./img/on.gif";
        }

        function off() {
            var img = document.getElementById('light');
            img.src = "./img/off.gif";
        }

        //2.输入框聚焦，展示小写；输入框失焦，展示大写
        function lower() {
            //获取输入框对象
            var input = document.getElementById('name');
            //把输入框的文本改为小写，先获取值，再改变数据
            input.value = input.value.toLowerCase()
        }

        function upper() {
            //获取输入框对象
            var input = document.getElementById('name');
            //把输入框的文本改为大写，先获取值，再改变数据
            input.value = input.value.toUpperCase()
        }
        //3.点击全选，把所有选项勾上；点击反选，取消所有勾选
        function checkAll() {
            var hobbys = document.getElementsByName('hobby');
            for (let index = 0; index < hobbys.length; index++) {
                const element = hobbys[index];
                element.checked = true;
            }
        }

        function reverse() {
            var hobbys = document.getElementsByName('hobby');
            for (let index = 0; index < hobbys.length; index++) {
                const element = hobbys[index];
                element.checked = false;
            }
        }
    </script>
</body>

</html>